package com.creditease.cn.monitor.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.creditease.cn.monitor.dao.redis.SlaveRedisNode;
import com.creditease.cn.monitor.service.ServerInfoService;

@Service
public class ServerInfoServiceImpl implements ServerInfoService{

	@Autowired
	private SlaveRedisNode slaveRedisNode;
	
	public Map<String,Map<String,String>> getServerInfo() {
		Map<String,Map<String,String>> result = new HashMap<String, Map<String,String>>();
		Map<String,String> serverMap = new HashMap<String, String>();
		Map<String,String> clientsMap = new HashMap<String, String>();
		Map<String,String> memoryMap = new HashMap<String, String>();
		Map<String,String> persistenceMap = new HashMap<String, String>();
		Map<String,String> replicationMap = new HashMap<String, String>();
		Map<String,String> statsMap = new HashMap<String, String>();
		Map<String,String> CPUMap = new HashMap<String, String>();
		Map<String,String> clusterMap = new HashMap<String, String>();
		Map<String,String> keyspaceMap = new HashMap<String, String>();
		
		Map<String,String> tempMap = null;
		Jedis jedis = slaveRedisNode.getConnection();
		String serverInfo = jedis.info();
		String[] serverInfos = serverInfo.split("\r\n");
		for(int i = 0;i<serverInfos.length;i++){
			if(serverInfos[i].contains(":")){
				String key = serverInfos[i].substring(0,serverInfos[i].indexOf(":"));
				String value = serverInfos[i].substring(serverInfos[i].indexOf(":")+1);
				tempMap.put(key, value);
			}else if(serverInfos[i].contains("#")){
				if(serverInfos[i].contains("Server")){
					tempMap = serverMap;
				}else if(serverInfos[i].contains("Clients")){
					tempMap = clientsMap;
				}else if(serverInfos[i].contains("Memory")){
					tempMap = memoryMap;
				}else if(serverInfos[i].contains("Persistence")){
					tempMap = persistenceMap;
				}else if(serverInfos[i].contains("Stats")){
					tempMap = statsMap;
				}else if(serverInfos[i].contains("Replication")){
					tempMap = replicationMap;
				}else if(serverInfos[i].contains("CPU")){
					tempMap = CPUMap;
				}else if(serverInfos[i].contains("Cluster")){
					tempMap = clusterMap;
				}else if(serverInfos[i].contains("Keyspace")){
					tempMap = keyspaceMap;
				}
			}
		}
		slaveRedisNode.close(jedis);
		
		result.put("Server", serverMap);
		result.put("Clients", clientsMap);
		result.put("Memory", memoryMap);
		result.put("Persistence", persistenceMap);
		result.put("Stats", statsMap);
		result.put("Replication", replicationMap);
		result.put("CPU", CPUMap);
		result.put("Cluster", clusterMap);
		result.put("Keyspace", keyspaceMap);
		return result;
	}

	public Map<String, String> getServerConfig() {
		Map<String,String> configMap = new HashMap<String, String>();
		Jedis jedis = slaveRedisNode.getConnection();
		List<String> configInfo = jedis.configGet("*");
		for(int i = 0;i<configInfo.size();i=i+2){
			configMap.put(configInfo.get(i), configInfo.get(i+1));
		}
		slaveRedisNode.close(jedis);
		return configMap;
	}
	
}
